package com.example.lenovo.weiyi;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.com.ruanmeng.demon.CommonM;
import com.com.ruanmeng.utils.CommonUtil;
import com.com.ruanmeng.utils.PreferencesUtils;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.rest.Request;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import nohttp.CallServer;
import nohttp.CustomHttpListener;
import share.Const;
import share.HttpIP;

/**
 * Created by Administrator on 2017/5/5.
 */

public class ChangeEmailActivity extends BaseActivity {
    @BindView(R.id.et_user_email)
    EditText etUserEmail;
    @BindView(R.id.bt_queding)
    Button btQueding;
    @BindView(R.id.iv_email_del)
    ImageView ivEmailDel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_changeemail);
        ButterKnife.bind(this);
        ChangLayTitle("邮箱");
        init();
    }

    private void init() {
        btQueding.setClickable(false);
        etUserEmail.addTextChangedListener(this);
        etUserEmail.setText(getIntent().getStringExtra("email"));
    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        super.onTextChanged(charSequence, i, i1, i2);
        if (CommonUtil.getEditText(etUserEmail).isEmpty()) {
            btQueding.setBackgroundResource(R.drawable.ed_fullgeray);
            btQueding.setClickable(false);
            ivEmailDel.setVisibility(View.GONE);
        } else {
            btQueding.setBackgroundResource(R.drawable.ed_fullmain);
            btQueding.setClickable(true);
            ivEmailDel.setVisibility(View.VISIBLE);
        }
    }

    @OnClick({R.id.bt_queding, R.id.iv_email_del})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_queding:
                if (TextUtils.isEmpty(etUserEmail.getText().toString())) {
                    CommonUtil.showToask(ChangeEmailActivity.this, "请输入您的邮箱！");
                    return;
                }
                if (!CommonUtil.isEmail(etUserEmail.getText().toString())) {
                    CommonUtil.showToask(ChangeEmailActivity.this, "请输入正确的邮箱！");
                    return;
                }
                getChangeData();
                break;
            case R.id.iv_email_del:
                etUserEmail.setText("");
                break;
        }

    }

    /**
     * 修改个人资料
     */
    private void getChangeData() {
        Request<String> mRequest = NoHttp.createStringRequest(HttpIP.UpdatePerMess, Const.POST);
        mRequest.addHeader("token", PreferencesUtils.getString(ChangeEmailActivity.this, "uid"));
        mRequest.add("email", etUserEmail.getText().toString());
        CallServer.getRequestInstance().add(ChangeEmailActivity.this, 0, mRequest,
                new CustomHttpListener(ChangeEmailActivity.this, true, CommonM.class) {

                    @Override
                    public void doWork(Object data, boolean isOne) {
                        CommonM model = ((CommonM) data);
                        PreferencesUtils.putString(ChangeEmailActivity.this, "email", etUserEmail.getText().toString());
                        finish();

                    }

                    @Override
                    public void onFinally(JSONObject obj) throws JSONException {
                        super.onFinally(obj);
                        if (!("100").equals(obj.getString("msgcode"))) {
                            CommonUtil.showToask(ChangeEmailActivity.this, obj.getString("msg"));
                        }
                    }
                }, true, true);
    }
}
